/*
 * AccountDetailsDialog.java
 *
 *  
 */
package org.msb.finance.app;

import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.GridBagConstraints.NONE;
import static java.awt.GridBagConstraints.NORTHWEST;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Currency;
import java.util.Iterator;

import javax.swing.*;

import org.msb.finance.data.Account;
import org.msb.finance.data.BankAccount;

/**
 * 
 * 
 * @author Marc Boudreau
 * 
 */
@SuppressWarnings("serial")
public abstract class AccountDetailsDialog extends JDialog {

	/*
	 * 
	 */
	private Account account;

	/*
	 * 
	 */
	private JLabel nameValue;

	/*
	 * 
	 */
	private JLabel typeValue;

	/*
	 * 
	 */
	private JComboBox institutionName;

	/*
	 * 
	 */
	private JTextArea descriptionValue;

	/*
	 * 
	 */
	private JTextField accountNumberValue;

	/*
	 * 
	 */
	private JCheckBox closedValue;

	/*
	 * 
	 */
	private JComboBox currencyCombo;

	/**
	 * @param owner
	 */
	protected AccountDetailsDialog(Window owner) {
		super(owner, ModalityType.APPLICATION_MODAL);
	}

	/**
	 * 
	 * @param window
	 * @param account
	 * 
	 * @return s
	 */
	public static AccountDetailsDialog createDialog(Window window, Account account) {
		if (account instanceof BankAccount) {
			return new BankAccountDetailsDialog(window);
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JDialog#dialogInit()
	 */
	@Override
	protected void dialogInit() {
		super.dialogInit();

		setContentPane(createContentPane());

		pack();
	}

	/*
	 * 
	 */
	private JComponent createContentPane() {
		JPanel contentPane = new JPanel(new GridBagLayout());
		contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		JLabel label = new JLabel(FinanceApp.getMessageResources().getString("dialog.accountdetails.name.caption")); //$NON-NLS-1$
		contentPane.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(0, 0, 0, 0), 0,
				0));

		this.nameValue = new JLabel();
		contentPane.add(this.nameValue, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, NORTHWEST, HORIZONTAL, new Insets(
				0, 5, 0, 0), 0, 0));

		contentPane.add(createRenameButton(), new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(
				0, 5, 0, 0), 0, 0));

		label = new JLabel(FinanceApp.getMessageResources().getString("dialog.accountdetails.type.caption")); //$NON-NLS-1$
		contentPane.add(label, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5, 0, 0, 0), 0,
				0));

		this.typeValue = new JLabel();
		contentPane.add(this.typeValue, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5, 5,
				0, 0), 0, 0));

		label = new JLabel(FinanceApp.getMessageResources().getString("dialog.accountdetails.institution.caption")); //$NON-NLS-1$
		contentPane.add(label, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5, 0, 0, 0), 0,
				0));

		this.institutionName = new JComboBox();
		contentPane.add(this.institutionName, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, NORTHWEST, HORIZONTAL,
				new Insets(5, 5, 0, 0), 0, 0));

		contentPane.add(createEditInstitutionButton(), new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, NORTHWEST, NONE,
				new Insets(5, 5, 0, 0), 0, 0));

		label = new JLabel(FinanceApp.getMessageResources().getString("dialog.accountdetails.description.caption")); //$NON-NLS-1$
		contentPane.add(label, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5, 0, 0, 0), 0,
				0));

		this.descriptionValue = new JTextArea(5, 20);
		contentPane.add(new JScrollPane(this.descriptionValue), new GridBagConstraints(1, 3, 2, 1, 0.0, 0.0, NORTHWEST,
				NONE, new Insets(5, 5, 0, 0), 0, 0));

		label = new JLabel(FinanceApp.getMessageResources().getString("dialog.accountdetails.accountnum.caption")); //$NON-NLS-1$
		contentPane.add(label, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5, 0, 0, 0), 0,
				0));

		this.accountNumberValue = new JTextField(20);
		contentPane.add(this.accountNumberValue, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, NORTHWEST, NONE,
				new Insets(5, 5, 0, 0), 0, 0));

		this.closedValue = new JCheckBox(FinanceApp.getMessageResources().getString(
				"dialog.accountdetails.accountclosed.cation")); //$NON-NLS-1$
		contentPane.add(this.closedValue, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5,
				5, 0, 0), 0, 0));

		label = new JLabel(FinanceApp.getMessageResources().getString("dialog.accountdetails.currency.caption")); //$NON-NLS-1$
		contentPane.add(label, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5, 0, 0, 0), 0,
				0));

		this.currencyCombo = new JComboBox();
		contentPane.add(this.currencyCombo, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(5,
				5, 0, 0), 0, 0));

		return contentPane;
	}

	/*
	 * 
	 */
	private JButton createRenameButton() {
		JButton button = new JButton(FinanceApp.getMessageResources().getString("dialog.accountdetails.rename.caption")); //$NON-NLS-1$
		button.addActionListener(new ButtonHandler());
		button.setActionCommand("rename"); //$NON-NLS-1$

		return button;
	}

	/*
	 * 
	 */
	private JButton createEditInstitutionButton() {
		JButton button = new JButton(FinanceApp.getMessageResources().getString(
				"dialog.accountdetails.editinstitution.caption")); //$NON-NLS-1$
		button.setActionCommand("editInstitution"); //$NON-NLS-1$

		return button;
	}

	/**
	 * 
	 * @param contentPane
	 * @param row
	 */
	protected abstract void addAccountTypeSpecificsToContentPane(JComponent contentPane, int row);

	/**
	 * 
	 * @param account
	 */
	public void editAccount(Account account) {

		this.nameValue.setText(account.getName());
		this.typeValue.setText(AccountTypeEnum.getTypeForClass(account.getClass()).getName());

		for (Iterator<Currency> it = FinanceApp.getCabinet().getCurrencies(); it.hasNext();) {
			this.currencyCombo.addItem(it.next());
		}

		this.currencyCombo.setSelectedItem(account.getCurrency());

		this.account = account;

		setVisible(true);
	}

	class ButtonHandler implements ActionListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();

			if (command.equals("rename")) { //$NON-NLS-1$
				renameAccount();
			} else if (command.equals("editInstitution")) { //$NON-NLS-1$
				editInstitution();
			}
		}
	}

	/*
	 * Handles the action of the Rename... button.
	 */
	private void renameAccount() {
		String newName = JOptionPane.showInputDialog(FinanceApp.getMessageResources().getString(
				"prompt.renameaccount.message")); //$NON-NLS-1$

		if (null != newName) {
			this.account.setName(newName);
			this.nameValue.setText(newName);
		}
	}

	/*
	 * 
	 */
	private void editInstitution() {
		//
	}
}
